package algorithms4g

import "fmt"

func init() {
	s := "abcefgeuricdqwsb"
	maxLen := longthOfLongestSubstring(s)
	fmt.Println("无重复字符的最长子串", maxLen)
}

/**
无重复字符的最长子串
*/
func longthOfLongestSubstring(s string) int {
	maxLen := 0
	hashMap := map[rune]int{}
	start := 0
	for end, v := range s {
		if i, ok := hashMap[v]; ok {
			start = max(start, i+1)
		}
		maxLen = max(maxLen, end-start+1)
		hashMap[v] = end
	}
	return maxLen
}

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
